<?php
/**
 * Created by zxk7516@foxmail.com  Date: 2017/10/20  Time: 16:05
 */

namespace App\Listeners;

use App\Events\SendTemplateMsg as Event;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Support\Log;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendTemplateMsg implements ShouldQueue
{
    public function handle(Event $msg)
    {
        $app = new Application(config('wechat'));

        $notice = $app->notice;
        $data = json_decode($msg->msg_data);
        $msg_id = $notice->to($msg->openid)->uses($msg->wx_template_id)
            ->addUrl($msg->url)
            ->data($data)
            ->send();
        Log::info("发送微信模板消息,：$msg_id; 模板数据: " . json_encode($msg));
    }

    public function failed(Event $msg, $event)
    {
        dd($msg);
        Log::info('发送失败: 模板数据' . json_encode($msg));
    }
}